import 'dart:io';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:scan/scan.dart';
import 'package:test01/okhttp_test/page/personalhomepage/about/about_page.dart';
import 'package:test01/okhttp_test/page/personalhomepage/setup/set_up_page.dart';
import 'package:test01/okhttp_test/page/personalhomepage/weight/my_weight_page.dart';
import 'package:test01/okhttp_test/pojo/attention_pojo.dart';
import 'package:test01/okhttp_test/pojo/userinfor_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';
import 'package:test01/okhttp_test/util/screen_util.dart';
import 'package:test01/silver_test/test01.dart';

import 'dynamic/my_dynamic_page.dart';
import 'feedback/Feedback_page.dart';
import 'my_attention_page.dart';
import 'my_fans_page.dart';


/*
*
*
* 我的---个人主页
*
*
* */



void main() {
  runApp(MyApp());
  //设置状态栏颜色为黑色
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
}

class MyApp extends StatefulWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    ///禁止程序横屏
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp, //只能纵向
      DeviceOrientation.portraitDown, //只能纵向
    ]);

    return MaterialApp(
      //国际化组件
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate, // 指定本地化的字符串和一些其他的值
        GlobalCupertinoLocalizations.delegate, // 对应的Cupertino风格
        GlobalWidgetsLocalizations.delegate // 指定默认的文本排列方向, 由左到右或由右到左
      ],
      supportedLocales: [
        Locale('zh', 'CN')
      ],
      home: PersonalHomePage(),
    );
  }
}

class PersonalHomePage extends StatefulWidget {
  const PersonalHomePage({Key key}) : super(key: key);

  @override
  _PersonalHomePageState createState() => _PersonalHomePageState();
}

class _PersonalHomePageState extends State<PersonalHomePage> {
  ///粉丝数量、关注数量
  GetFollowCounts getFollowCounts=GetFollowCounts();
  ///体重、相册数量
  UserWBodyInformation userWBodyInformation=UserWBodyInformation();
  ///用户基础信息
  UserBaseInfoDto userBaseInfoDto=UserBaseInfoDto();

  ///高斯模糊
  Widget vague() {
    ///沉侵式状态栏
    if (Platform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle =
          SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }

    return Container(
        child: Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).padding.top + 79 + 25 + 23,
          child: Image.network(
            userBaseInfoDto.avatarUri,
            fit: BoxFit.cover,
          ),
        ),
        Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).padding.top + 79 + 25 + 23,
          color: Colors.white.withOpacity(0.5),
        ),
        BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 25.0, sigmaY: 25.0),
          child: Column(
            children: <Widget>[
              //头像图标-昵称
              headportrait(),
              //粉丝数量
              fansnumber(),
            ],
          ),
        )
      ],
    ));
  }

  ///头像图标-昵称
  Widget headportrait() {
    return Container(
      alignment: Alignment.center,
      height: 71,
      margin: EdgeInsets.only(
          left: 16, top: MediaQuery.of(context).padding.top + 41),
      child: Row(
        children: <Widget>[
          Container(
            width: 71,
            height: 71,
            child: Stack(
              children: <Widget>[
                Container(
                  child: ClipOval(
                    child: Image.network(
                      userBaseInfoDto.avatarUri,
                      height: 71,
                      width: 71,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 47, top: 47),
                  child: Image(
                    width: 24,
                    height: 24,
                    image: AssetImage(
                        "assets/personal_home_icon/picture_icon.png"),
                  ),
                )
              ],
            ),
          ),
          //头像左侧文字
          Container(
            margin: EdgeInsets.only(left: 12),
            width: MediaQuery.of(context).size.width - 16 - 16 - 71 - 12,
            child: Text(
              userBaseInfoDto.nickName,
              style:
                  TextStyle(fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.left,
            ),
          ),
        ],
      ),
    );
  }

  ///粉丝数量
  Widget fansnumber() {
    return Container(
      height: 60,
      margin: EdgeInsets.only(top: 12),
      child: Row(
        children: <Widget>[
          //关注
          GestureDetector(
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return MyAttentionPage();
              }));
            },
            child: Container(
              width: MediaQuery.of(context).size.width / 3,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    "${getFollowCounts.followingCount}",
                    style: TextStyle(
                        fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
                  ),
                  Text(
                    "关注",
                    style: TextStyle(
                        fontSize: 12, color: Color.fromRGBO(102, 102, 102, 1)),
                  )
                ],
              ),
            ),
          ),
          //粉丝
          GestureDetector(
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return FansPage();
              }));
            },
            child: Container(
              width: MediaQuery.of(context).size.width / 3,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    "${getFollowCounts.followerCount}",
                    style: TextStyle(
                        fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
                  ),
                  Text(
                    "粉丝",
                    style: TextStyle(
                        fontSize: 12, color: Color.fromRGBO(102, 102, 102, 1)),
                  )
                ],
              ),
            ),
          ),
          //动态
          GestureDetector(
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return NetScrollHomePage();
              }));
            },
            child: Container(
              width: MediaQuery.of(context).size.width / 3,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    "${getFollowCounts.feedCount}",
                    style: TextStyle(
                        fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
                  ),
                  Text(
                    "动态",
                    style: TextStyle(
                        fontSize: 12, color: Color.fromRGBO(102, 102, 102, 1)),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  ///健身目录-健身相册
  Widget bodybuilding() {
    return Row(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(left: 16, top: 20),
          width: (MediaQuery.of(context).size.width - 16 - 16 - 16) / 2,
          child: Text(
            "体重记录",
            style:
                TextStyle(fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)),
          ),
        ),
        Container(
          margin: EdgeInsets.only(left: 16, top: 20),
          width: (MediaQuery.of(context).size.width - 16 - 16 - 16) / 2,
          child: Text(
            "健身相册",
            style:
                TextStyle(fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)),
          ),
        )
      ],
    );
  }

  ///健身数据
  Widget bodybuildingnumber() {
    return Row(
      children: <Widget>[
        GestureDetector(
          onTap: () {
            print("-----------------------体重数据---------------------");
            Navigator.push(context, MaterialPageRoute(builder: (context) {
              return MyWieghtPage();
            }));
          },
          child: Container(
            height: 103.5,
            margin: EdgeInsets.only(left: 16, top: 7.5),
            width: (MediaQuery.of(context).size.width - 16 - 16 - 16) / 2,
            child: Stack(
              children: <Widget>[
                Container(
                  child: Image.asset(
                      "assets/personal_home_icon/weight_background.png"),
                ),
                Container(
                  margin: EdgeInsets.only(left: 16, top: 16),
                  child: Text(
                    "${userWBodyInformation.weight}",
                    style: TextStyle(
                        fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)),
                  ),
                ),
                Container(
                  width: 18,
                  height: 18,
                  margin: EdgeInsets.only(left: 16, top: 65.5),
                  child: Image.asset(
                      "assets/personal_home_icon/weight_record.png"),
                )
              ],
            ),
          ),
        ),
        GestureDetector(
          onTap: () {
            print("-----------------------健身----------------------");
          },
          child: Container(
            height: 103.5,
            margin: EdgeInsets.only(left: 16, top: 7.5),
            width: (MediaQuery.of(context).size.width - 16 - 16 - 16) / 2,
            child: Stack(
              children: <Widget>[
                Container(
                  child: Image.asset(
                      "assets/personal_home_icon/bodybuilding_background.png"),
                ),
                Container(
                  margin: EdgeInsets.only(left: 16, top: 16),
                  child: Text(
                    "${userWBodyInformation.albumNum}",
                    style: TextStyle(
                        fontSize: 14, color: Color.fromRGBO(51, 51, 51, 1)),
                  ),
                ),
                Container(
                  width: 18,
                  height: 18,
                  margin: EdgeInsets.only(left: 16, top: 65.5),
                  child: Image.asset(
                      "assets/personal_home_icon/bodybuilding_album.png"),
                )
              ],
            ),
          ),
        ),
      ],
    );
  }

  ///扫一扫
  Widget scan() {
    return GestureDetector(
      onTap: () {
        print("---------------------------扫一扫---------------------");
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return ScanCodePage();
        }));
      },
      child: Container(
        height: 48,
        width: MediaQuery.of(context).size.width,
        margin: EdgeInsets.only(top: 36),
        child: Row(
          children: <Widget>[
            Container(
              width: 24.04,
              height: 24.04,
              margin: EdgeInsets.only(left: 16),
              child: Image.asset(
                "assets/personal_home_icon/saoyisao.png",
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 12),
              child: Text(
                "扫一扫",
                style: TextStyle(
                    fontSize: 16, color: Color.fromRGBO(34, 34, 34, 1)),
              ),
            ),
            Spacer(),
            Container(
              child: Icon(
                Icons.arrow_forward_ios_sharp,
                size: 18,
                color: Color.fromRGBO(90, 90, 90, 1),
              ),
            ),
            SizedBox(
              width: 16,
            )
          ],
        ),
      ),
    );
  }

  ///关于
  Widget abouts() {
    return GestureDetector(
      onTap: () {
        print("---------------------------关于-----------------------");
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return AboutPage();
        }));
      },
      child: Container(
        height: 48,
        width: MediaQuery.of(context).size.width,
        child: Row(
          children: <Widget>[
            Container(
              width: 24.04,
              height: 24.04,
              margin: EdgeInsets.only(left: 16),
              child: Image.asset(
                "assets/personal_home_icon/guanyu.png",
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 12),
              child: Text(
                "关于",
                style: TextStyle(
                    fontSize: 16, color: Color.fromRGBO(34, 34, 34, 1)),
              ),
            ),
            Spacer(),
            Container(
              child: Icon(
                Icons.arrow_forward_ios_sharp,
                size: 18,
                color: Color.fromRGBO(90, 90, 90, 1),
              ),
            ),
            SizedBox(
              width: 16,
            )
          ],
        ),
      ),
    );
  }

  ///意见反馈
  Widget feedback() {
    return GestureDetector(
      onTap: () {
        print("----------------------------意见反馈--------------------");
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return FeedbackPage();
        }));
      },
      child: Container(
        height: 48,
        width: MediaQuery.of(context).size.width,
        child: Row(
          children: <Widget>[
            Container(
              width: 24.04,
              height: 24.04,
              margin: EdgeInsets.only(left: 16),
              child: Image.asset(
                "assets/personal_home_icon/yijian.png",
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 12),
              child: Text(
                "意见反馈",
                style: TextStyle(
                    fontSize: 16, color: Color.fromRGBO(34, 34, 34, 1)),
              ),
            ),
            Spacer(),
            Container(
              child: Icon(
                Icons.arrow_forward_ios_sharp,
                size: 18,
                color: Color.fromRGBO(90, 90, 90, 1),
              ),
            ),
            SizedBox(
              width: 16,
            )
          ],
        ),
      ),
    );
  }

  ///设置
  Widget install() {
    return GestureDetector(
      onTap: () {
        print("---------------------------设置-------------------------");
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return SetUpPage();
        }));
      },
      child: Container(
        height: 48,
        width: MediaQuery.of(context).size.width,
        child: Row(
          children: <Widget>[
            Container(
              width: 24.04,
              height: 24.04,
              margin: EdgeInsets.only(left: 16),
              child: Image.asset(
                "assets/personal_home_icon/shezhi.png",
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 12),
              child: Text(
                "设置",
                style: TextStyle(
                    fontSize: 16, color: Color.fromRGBO(34, 34, 34, 1)),
              ),
            ),
            Spacer(),
            Container(
              child: Icon(
                Icons.arrow_forward_ios_sharp,
                size: 18,
                color: Color.fromRGBO(90, 90, 90, 1),
              ),
            ),
            SizedBox(
              width: 16,
            )
          ],
        ),
      ),
    );
  }


  ///获取我的界面用户信息
  mynumbers() async{
    //获取关注数、粉丝数、点赞数
    getFollowCounts =
    await getFollowCount(interface: "/appuser/web/user/getFollowCount");
    //获取用户附加信息(体重、相册数量）
    userWBodyInformation=await getExtraInfo(interface: "/appuser/web/user/getExtraInfo");
    //获取用户基础信息
    userBaseInfoDto=await getUserBaseInfo(interface: "/ucenter/web/user/getUserBaseInfo");
    print("---------------用户数据-------------------------");
    setState(() {

    });
  }



  @override
  void initState(){
    // TODO: implement initState\
    super.initState();
    mynumbers();
   ScreenUtil.init();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        ///界面滚动
        body:userBaseInfoDto.uid==null?Container():CustomScrollView(
          slivers: <Widget>[
            SliverList(
              delegate: new SliverChildListDelegate(
                [Column(
                  children: <Widget>[
                    vague(),
                    //健身记录-健身相册
                    bodybuilding(),
                    //健身数据
                    bodybuildingnumber(),
                    //扫一扫
                    scan(),
                    //关于
                    abouts(),
                    //意见反馈
                    feedback(),
                    //设置
                    install()
                  ],
                )
                ],
              ),
            ),
          ],
        )

    );
  }
}


/*
*
* 我的---扫码界面
*
* */
class ScanCodePage extends StatefulWidget {
  const ScanCodePage({Key key}) : super(key: key);

  @override
  _ScanCodePageState createState() => _ScanCodePageState();
}

class _ScanCodePageState extends State<ScanCodePage> {
  ScanController _scanController= ScanController();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
     body: ScanView(
       controller: _scanController,
       scanAreaScale: .7,
       scanLineColor: Colors.green.shade400,
       onCapture: (data) {
         print("---------------扫码结果$data--------------");
       },
     ),
    );
  }
}

